﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;


public partial class Admin_FinSav_FinSav005 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinReceipt finReceipt = new FinReceipt();
        FinBox finBox = new FinBox();
        FinBoxReceipts finBoxReceipts = new FinBoxReceipts();
        string user = LoginUserInfo.CurrentUser.LoginName;
        DateTime today = DateTime.Now;
        string companyCode = drpCompany.SelectedValue;
        
        //if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        //{
        //    BFCompanyDic bFCompanyDic = new BFCompanyDic();
        //    bFCompanyDic.LoadByPrimaryKey(decimal.Parse(drpCompany.SelectedValue));
        //    if (bFCompanyDic.es.HasData)
        //    {
        //        companyCode = bFCompanyDic.CompanyCode;
        //    }
        //}



        using (esTransactionScope tr = new esTransactionScope())
        {
            finBox.BoxNo = txtBoxNo.Text.Trim();
            finBox.DocumentType = drpDocumentType.SelectedValue;
            finBox.FunctionDept = drpFunctionDept.SelectedValue;
            if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
            { finBox.BoxLocation = decimal.Parse(drpLocation.SelectedValue); }
            finBox.StorageStatus = drpStorageStatus.SelectedValue;
            finBox.SupplierBoxNo = txtSupplierBoxNo.Text.Trim();
            finBox.CreatedBy = finBox.UpdatedBy = user;
            finBox.CreatedDttm = finBox.UpdatedDttm = today;
            finBox.VersionNo = 0;
            finBox.CompanyCode = companyCode;
            finBox.Save();

            finReceipt.FunctionDept = drpFunctionDept.SelectedValue;
            finReceipt.StorageStatus = drpStorageStatus.SelectedValue;
            finReceipt.EStorageStatus = drpEStorageStatus.SelectedValue;
            if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
            { finReceipt.StorageLocation = decimal.Parse(drpLocation.SelectedValue); }
            if (!string.IsNullOrEmpty(txtNeedStoreYear.Text.Trim()))
            { finReceipt.NeedStoreYear = int.Parse(txtNeedStoreYear.Text.Trim()); }
            //if (!string.IsNullOrEmpty(txtDestoryDate.Value))
            //{ finReceipt.DestoryDate = DateTime.Parse(txtDestoryDate.Value); }
            finReceipt.Comments = txtComments.Text.Trim();

            finReceipt.HisIndexStart = txtHisIndexStart.Text.Trim();
            finReceipt.HisIndexEnd = txtHisIndexEnd.Text.Trim();
            finReceipt.HisDocumentStartDate = txtHisDocumentStartDate.Text.Trim();
            finReceipt.HisDocumentEndDate = txtHisDocumentEndDate.Text.Trim();
            finReceipt.CreatedBy = finReceipt.UpdatedBy = user;
            finReceipt.CreatedDttm = finReceipt.UpdatedDttm = today;
            finReceipt.VersionNo = 0;
            finReceipt.Save();

            finBoxReceipts.BoxID = finBox.BoxID;
            finBoxReceipts.ReceiptID = finBoxReceipts.ReceiptID;
            finBoxReceipts.CreatedBy = finBoxReceipts.UpdatedBy = user;
            finBoxReceipts.CreatedDttm = finBoxReceipts.UpdatedDttm = today;
            finBoxReceipts.Save();

            tr.Complete();

            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNew();", true);
        }
    }
}